package com.xfn.controller;

import com.alibaba.fastjson.JSONObject;
import com.xfn.service.ILoginService;
import com.xfn.service.ILoginServiceApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.RenderedImage;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping(value = "/login")
public class LoginController {
    @Autowired(required = false)
    private ILoginService iLoginService;

    @Autowired(required = false)
    private ILoginServiceApi iLoginServiceApi;

    /**
     * 校验验证码
     * @param randomStr
     * @param request
     * @return
     */
    @PostMapping(value = "/validateImageCodes")
    public JSONObject validateImageCodes(String randomStr, HttpServletRequest request){
        String code = request.getSession().getAttribute("code").toString();
        JSONObject jsonObject = new JSONObject();
        if (randomStr != null && !"".equals(randomStr) && code != null && !code.equals("")) {
            if (code.equalsIgnoreCase(randomStr)) {
                jsonObject.put("Status", "0");
                jsonObject.put("Redesc", "验证通过");
            } else {
                jsonObject.put("Status", "1");
                jsonObject.put("Redesc", "验证错误");
            }
        } else {
            jsonObject.put("Status", "1");
            jsonObject.put("Redesc", "验证失败");
        }
        return jsonObject;
    }

    /**
     * 验证码生成
     */
    @GetMapping(value = "/verificationCode")
    public void verificationCode(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = iLoginService.generateCodeAndPic();
        HttpSession session = request.getSession();
        session.setAttribute("code", map.get("code").toString());
        ServletContext application = request.getServletContext();
        application.setAttribute("verCode", map.get("code").toString());

        // 禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", -1);

        ServletOutputStream sos;

        try {
            sos = response.getOutputStream();
            ImageIO.write((RenderedImage) map.get("codePic"), "jpeg", sos);
            sos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    @PostMapping(value = "/login.do")
    public JSONObject login(String username,String password,HttpServletRequest request){
        Map<String,String> map = iLoginServiceApi.login(username,password);
        JSONObject jsonObject  = new JSONObject();
        /*ServletContext application = request.getServletContext();*/
        /*request.getSession().setAttribute("userName",username);*/
        /*application.setAttribute("userName",username);*/
        if(map!=null){
            jsonObject.put("Status","0000");
            jsonObject.put("ResDes","成功");
            request.getSession().setAttribute("userName",username);
            request.getSession().setAttribute("password",password);
        }else {
            jsonObject.put("Status","9999");
            jsonObject.put("ResDes","失败");
        }
        return jsonObject;
    }
}
